<template>
    <el-dialog v-model="show" :title="title" align-center width="20%" @close="handleClose">
        <el-form :model="form" ref="formRef" :rules="rules" label-width="auto">
            <el-form-item label="对应类别：" prop="orderType">
                <el-select v-model="form.orderType" placeholder="请选择对应类别" size="large" style="width: 240px">
                    <el-option v-for="dict in customized_order_type" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="工序名称：" prop="workProcedureId">
                <el-select v-model="form.workProcedureId" placeholder="请选择工序名称" size="large" style="width: 240px">
                    <el-option v-for="dict in props.allList" :key="dict.id" :label="dict.workName" :value="dict.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="工序排序：" prop="sort">
                <el-input v-model="form.sort" style="width: 240px" placeholder="请输入工序排序" />
            </el-form-item>
            <el-form-item label="默认选择：" prop="defaultFlag">
                <el-select v-model="form.defaultFlag" placeholder="请选择默认选择" size="large" style="width: 240px">
                    <el-option label="是" value="1" />
                    <el-option label="否" value="0" />
                </el-select>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="handleClose">取 消</el-button>
                <el-button type="primary" @click="handleSubmit(formRef)">确 认</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { type FormInstance, FormRules } from 'element-plus';
import { editWorkingProcedureApi, addWorkingProcedureApi } from '@/api/sj/productManagement/finalDraft';
import { workingProcedureType } from '@/api/sj/productManagement/finalDraft/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { customized_order_type } = toRefs<any>(proxy?.useDict('customized_order_type'));

const props = defineProps(['visible', 'initList', 'ids', 'allList']);
const emits = defineEmits(['submit', 'close']);
const formRef = ref<FormInstance>();
const form = ref<workingProcedureType>({ customizedTypeId: "", orderType: '', workProcedureId: '', sort: '', defaultFlag: '' });
const title = ref('新增工序详情');
const show = ref(false);

watch(() => props.visible, async (newValue) => {
    show.value = newValue;
}, { deep: true });

watch(() => props.initList, async (newValue) => {
    if (newValue) {
        form.value = newValue;
        title.value = '修改工序详情';
    };
}, { deep: true });


const handleSubmit = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate(async (valid) => {
        if (valid) {
            if (form.value.id) {
                form.value.customizedTypeId = props.ids
                form.value.sort = Number(form.value.sort)
                const { code } = await editWorkingProcedureApi({ ...form.value });
                if (code == 200) {
                    emits('submit', true);
                    proxy?.$modal.msgSuccess('修改成功');
                    handleClose();
                } else {
                    emits('submit', false);
                };
            } else {
                form.value.customizedTypeId = props.ids
                form.value.sort = Number(form.value.sort)
                const { code } = await addWorkingProcedureApi({ ...form.value });
                if (code == 200) {
                    emits('submit', true);
                    proxy?.$modal.msgSuccess('新增成功');
                    handleClose();
                } else {
                    emits('submit', false);
                };
            }
        }
    });
};


const handleClose = () => {
    formRef.value?.resetFields();
    emits('close');
};

const rules: FormRules = reactive({
    orderType: [
        { required: true, message: '请选择对应类别', trigger: 'change' },
    ],
    workProcedureId: [
        { required: true, message: '请选择工序名称', trigger: 'change' },
    ],
    sort: [
        { required: true, message: '请输入工序排序', trigger: 'blur' },
    ],
    defaultFlag: [
        { required: true, message: '请选择默认选择', trigger: 'change' },
    ],
});

onMounted(() => {
})
</script>

<style lang="scss" scoped>
.el-select {
    width: 100%;
}
</style>